The same as power, for whole numbers: value multiplied by itself exponent times, and the answer is a whole number. power_int(2, 3) is 8 and power_int(10, 6) is 1000000 - a whole number goes much further than a real, which has already run out by 10 to the power of 6. Both numbers have to be whole, as the exponent already has to be for power.
power_int(value, exponent) -> int| Name | Type |
|---|---|
value | int |
exponent | int |
int
The same as power, for whole numbers: value multiplied by itself exponent times - and the answer is a whole number. power_int(2, 3) is 8 (2 times 2 times 2) and power_int(10, 2) is 100.
Reach for this one whenever the number is counted rather than measured: how many cells a grid of a given depth has, how many frames a level takes, the place value of a digit when you split a score into digits. Use power when the number has a fraction.
var digits = 6;
var unit = power_int(10, digits);unit is 1000000. A real runs out at about 524,000, so power(10, 6) has already gone wrong; power_int(10, 6) has not. unit is a whole number, so it can be handed to anything that wants one.
power and power_int do the same arithmetic. They differ in the type of the answer, and that difference is worth a function of its own because of what goes wrong without it.
power hands back a real. So this line:
unit = power(10, 3); <- the trap: this is NOT what you want hereturns unit into a real - the value is still 1000, but the kind of number has changed. Nothing complains there. The complaint arrives later, somewhere else, wherever unit is next handed to something that wants a whole number, such as the frame of draw_sprite, which says which picture of a sprite to show:
Argument 4 to `draw_sprite` should be an int, but this is a real.That error points at a line that looks perfectly fine, and it may be in a different file from the assignment that caused it. Written with power_int, it cannot happen.
power_int takes whole numbers only - the exponent already had to be one for power, and now the number being multiplied does too. A number with a point in it is a real - even 4.0 - and is refused when the project is checked:
Argument 1 to `power_int` should be an int, but this is a real.That is what you get from power_int(2.5, 3). If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round. If the number really is measured, use power instead.
| You write | You get | Why |
|---|---|---|
power_int(5, 0) | 1 | anything to the power of nothing is 1 |
power_int(0, 0) | 1 | the usual rule, so power_int(x, 0) is 1 for every x |
power_int(-2, 3) | -8 | a minus number is fine; an odd exponent keeps the minus, an even one (power_int(-2, 2) is 4) drops it |
power_int(2, -1) | 0 | a minus exponent is one divided by the power, and a whole number cannot hold the 0.5 that power gives, so it is cut toward zero |
power_int(1, -5) | 1 | one divided by 1 is still the whole number 1 |
power_int(-1, -3) | -1 | and so is one divided by -1 |
power_int(0, -1) | 0 | one divided by nothing has no answer, so it gives 0 instead of stopping the game |
A whole number runs out at about 2.1 billion. power_int(10, 9) is 1,000,000,000 and right. power_int(10, 10) should be 10,000,000,000, and comes out as 1,410,065,408 instead; power_int(2, 31) comes out as -2,147,483,648. There is no error and no warning - the answer simply wraps round, the same as for any other whole-number sum that grows past its edge.
A bigger exponent on its own is fine: power_int(2, 1000000000) is answered at once (the work grows with how many binary digits the exponent has, not with its size), though the answer to that one is of course far past the edge.
About one multiply for every binary digit of the exponent - power_int(x, 10) is three squarings and two more multiplies, not ten of them. Cheap enough for every frame.
power and power_int differ in TYPE, not in arithmetic. power hands back a real, so `unit = power(10, digits)` turns a whole number into a real one, and the next place that wants a whole number refuses it - usually somewhere else entirely, which makes it a puzzle. power_int hands back a whole number, so it cannot happen. Both numbers have to be whole: power_int(2.5, 3) is refused when the project is checked, and so is a variable that holds a real, even one that happens to hold 4.0 - round it first with int_floor, int_ceil or int_round. It works by square-and-multiply, so even power_int(2, 1000000000) is a few dozen multiplies, never a frozen console. exponent 0 gives 1 (power_int(0, 0) is 1 too). A negative value is fine: power_int(-2, 3) is -8. A negative exponent is one over the positive power, and a whole number cannot hold a fraction, so the answer is cut toward zero: power_int(2, -1) is 0, where power says 0.5. Only 1 and -1 stay whole: power_int(1, -5) is 1 and power_int(-1, -3) is -1. power_int(0, -1) has no answer and gives 0. The ceiling is 2,147,483,647: power_int(10, 9) is 1,000,000,000 and right, but power_int(10, 10) comes out as 1,410,065,408 instead of 10,000,000,000, silently, wrapping round.